package com.example.mobileyd20313;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

public class LoginActivity extends AppCompatActivity {

    private ImageView imageView;
    private RelativeLayout relativeLayout;
    private TextView textView;
    private TextView tvSetting;
    private EditText editUser;
    private EditText editPwd;
    private CheckBox checkBoxpwd;
    private CheckBox checkBoxauto;
    private Button btnLogin;
    private Button button;
    private SharedPreferences sp;

    protected void onResume() {
        super.onResume();
        boolean isSavePwd=sp.getBoolean("isSavePwd",false);
        checkBoxpwd.setChecked(isSavePwd);
        boolean isAutoLogin=sp.getBoolean("isAutoLogin",false);
        checkBoxauto.setChecked(isAutoLogin);
        String userName = sp.getString("userName","");
        String userPwd = sp.getString("userPwd","");
        if (isSavePwd){
            editUser.setText(userName);
            editPwd.setText(userPwd);
        }
        if (isAutoLogin){
            if (!userName.equals("")&!userPwd.equals("")){
                btnLogin.performClick();
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        checkBoxpwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("isSavePwd",isChecked);
                editor.commit();
            }
        });
        checkBoxauto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("isSavePwd",isChecked);
                editor.putBoolean("isAutoLogin",isChecked);
                editor.commit();
            }
        });
        sp=getSharedPreferences("loginSetting",MODE_PRIVATE);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName=editUser.getText().toString().trim();
                String userPwd=editPwd.getText().toString().trim();
                HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                    @Override
                    public void onFailure(Exception e) {
                        Log.i("LoginLog",e.getMessage());
                    }

                    @Override
                    public void onResponse(String json) {
                        LoginBean loginBean = new Gson().fromJson(json,LoginBean.class);
                        if (loginBean.getRESULT().equals("S")){
                            SharedPreferences.Editor editor=sp.edit();
                            if (checkBoxpwd.isChecked()){
                                editor.putString("userName",userName);
                                editor.putString("userPwd",userPwd);
                            }else {
                                editor.putString("userName",null);
                                editor.putString("userPwd",null);
                            }
                            editor.commit();
                            Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                            startActivity(intent);
                            return;
                        }else if (loginBean.getRESULT().equals("F")){
                            Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                },"user_login",new String[] {"UserName","UserPwd"},userName,userPwd);
            }
        });
    }

    private void initView() {
        imageView = (ImageView) findViewById(R.id.imageView);
        relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
        textView = (TextView) findViewById(R.id.textView);
        tvSetting = (TextView) findViewById(R.id.tv_setting);
        editUser = (EditText) findViewById(R.id.edit_user);
        editPwd = (EditText) findViewById(R.id.edit_pwd);
        checkBoxpwd = (CheckBox) findViewById(R.id.checkBoxpwd);
        checkBoxauto = (CheckBox) findViewById(R.id.checkBoxauto);
        btnLogin = (Button) findViewById(R.id.btn_login);
        button = (Button) findViewById(R.id.button);
    }
}